package com.visionblue.business.ship.page.register

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.visionblue.business.ship.R
import com.visionblue.business.ship.base.BaseVMActivity
import com.visionblue.business.ship.page.register.config.MenuConfigFragment
import com.visionblue.business.ship.page.register.config.PackageInfoGet
import com.visionblue.business.ship.page.register.register.RegisterFragment
import com.visionblue.business.ship.page.register.register.RegisterViewModel
import com.visionblue.business.ship.util.toast
import kotlinx.android.synthetic.main.activity_register.*
import org.koin.androidx.viewmodel.ext.android.getViewModel

/**
 * @author: xlh
 * @date: 2020-06-07 9:17 PM
 * @desc:
 */
class RegisterActivity : BaseVMActivity<RegisterViewModel>() {

    override fun initVM(): RegisterViewModel {
        return getViewModel()
    }

    override val layoutId: Int
        get() = R.layout.activity_register

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        rbBasic.paint.isFakeBoldText = true
        rbConfig.paint.isFakeBoldText = true

        val registerFragment = RegisterFragment.newInstance()
        val menuConfigFragment = MenuConfigFragment.newInstance()

        supportFragmentManager.beginTransaction()
            .add(R.id.flContent, registerFragment)
            .add(R.id.flContent, menuConfigFragment)
            .commit()

        rgTabs.setOnCheckedChangeListener { _, checkedId ->
            when(checkedId){
                R.id.rbBasic -> {
                    supportFragmentManager.beginTransaction()
                        .hide(menuConfigFragment)
                        .show(registerFragment)
                        .commit()
                }
                R.id.rbConfig -> {
                    toast { "当前版本V" + PackageInfoGet.getAppVersionName(this) }
                    supportFragmentManager.beginTransaction()
                        .hide(registerFragment)
                        .show(menuConfigFragment)
                        .commit()
                }
            }
        }

        ivBack.setOnClickListener {
            doBack()
        }

        rgTabs.check(R.id.rbBasic)

    }

    private fun doBack() {
        supportFragmentManager.fragments.forEach { frag ->
            if (frag is RegisterFragment) {
                frag.onBackClick()
            }
        }
    }


    override fun onBackPressed() {
        doBack()
    }

    companion object {
        fun start(context: Activity, code: Int) {
            Intent(context, RegisterActivity::class.java)
                .run {
                    context.startActivityForResult(this, code)
                }
        }
    }
}